//https://www.nowcoder.com/practice/0e26e5551f2b489b9f58bc83aa4b6c68?tpId=13&tqId=1374738&ru=/exam/oj/ta&qru=/ta/coding-interviews/question-ranking&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D1%26tpId%3D13%26type%3D13

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 
     * @param s string字符串 
     * @return string字符串
     */
    string replaceSpace(string s) {
        // write code here
        string res="";
        if(s.empty()) return res;
        for(size_t i=0;i<s.size();++i) {
            if (s[i] == ' ') {
                res=res+"%20";
            }else{
                res=res+s[i];
            }
        }
        return res;
    }
};
class Solution2 {
  public:
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param s string字符串
     * @return string字符串
     */
    string replaceSpace(string s) {

        // write code here
        if (s.empty()) return "";
        // 需要提前计算空格的数量
        int number = 0;
        for (int i = 0 ; i < s.length(); ++i) {
            if (s.at(i) == ' ') {
                ++number;
            }
        }
        std::string t_char;
        t_char.resize(s.length() + number * 2);
        int index = 0;
        // 开始遍历拷贝自负元素
        for (int j = 0 ; j < s.length(); ++j) {
            if (s.at(j) == ' ') {
                t_char[index++] = '%';
                t_char[index++] = '2';
                t_char[index++] = '0';
            } else {
                t_char[index++] = s.at(j);
            }
        }

        return  t_char;
    }
};